package com.aygxy.carproject.controller;


import com.aygxy.carproject.pojo.product.dto.SpuAddNewDTO;
import com.aygxy.carproject.pojo.product.dto.SpuUpdateDTO;
import com.aygxy.carproject.pojo.product.vo.SpuListItemVO;
import com.aygxy.carproject.pojo.product.vo.SpuStandardVO;
import com.aygxy.carproject.service.ProductSpuService;
import com.aygxy.carproject.web.JsonPage;
import com.aygxy.carproject.web.JsonResult;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(tags = "06. 产品Spu管理模块")
@RestController
@RequestMapping("/products")
@Slf4j
public class ProductSpuController {
    @Autowired
    ProductSpuService productSpuService;

    /**
     * 新增SPU
     */
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "新增SPU", notes = "需要商品后台【写】权限：/pms/product/update")
   // @PreAuthorize("hasAuthority('/pms/product/update')")
    @PostMapping("/addnew")
    public JsonResult<Void> addNew(@Valid SpuAddNewDTO spuAddnewDTO) {
        productSpuService.addNew(spuAddnewDTO);
        return JsonResult.ok();
    }

    /**
     * 更新SPU
     */
    @ApiOperationSupport(order = 30)
    @ApiOperation(value = "更新SPU", notes = "需要商品后台【写】权限：/pms/product/update")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "SPU id", paramType = "path",required = true, dataType = "long")
    })
   // @PreAuthorize("hasAuthority('/pms/product/update')")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> updateById(@PathVariable("id") Long id, @Valid SpuUpdateDTO spuDTO) {
        productSpuService.updateById(id, spuDTO);
        return JsonResult.ok();
    }


    @ApiOperation("根据类别id分页查询spu数据")
    @ApiOperationSupport(order = 100)
    @GetMapping("/category/{id:[0-9]+}/spu")
    public JsonResult<JsonPage<SpuListItemVO>> getSpuByCategoryId(@PathVariable("id") Long categoryId, Integer page, Integer pageSize){
      log.info("开始执行根据类别id查询spu数据操作！！");
        JsonPage<SpuListItemVO> spuListItemVOJsonPage = productSpuService.listSpuByCategoryId(categoryId, page, pageSize);
        return JsonResult.ok(spuListItemVOJsonPage);
    }

    /**
     * 查询SPU详情
     */
    @ApiOperationSupport(order = 200)
    @ApiOperation(value = "查询SPU详情", notes = "需要商品后台【读】权限：/pms/product/read")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "SpuId", required = true, dataType = "long")
    })
    //@PreAuthorize("hasAuthority('/pms/product/read')")
    @GetMapping("/{id:[0-9]+}/spu")
    public JsonResult<SpuStandardVO> getById(@PathVariable Long id) {
        return JsonResult.ok(productSpuService.getSpuById(id));
    }


}
